package com.lesso.lzh.sunflower.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.lesso.lzh.sunflower.core.ApiResponse;
import com.lesso.lzh.sunflower.pojo.dto.DeviceDTO;
import com.lesso.lzh.sunflower.pojo.dto.DeviceQueryDTO;
import com.lesso.lzh.sunflower.pojo.vo.DeviceVO;
import com.lesso.lzh.sunflower.pojo.vo.EmployeeVO;
import com.lesso.lzh.sunflower.service.IDeviceService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 设备信息 前端控制器
 * </p>
 *
 * @author zqz
 * @since 2025-07-04
 */
@RestController
@RequestMapping("/device")
public class DeviceController {
    @DubboReference
    private IDeviceService deviceService;

    @PostMapping
    public ApiResponse<Long> save(@RequestBody DeviceDTO dto) {
        try {
            return ApiResponse.<Long>ok().data(deviceService.save(dto)).build();
        } catch (Exception e) {
            return ApiResponse.<Long>error().msg("保存设备信息失败: " + e.getMessage()).build();
        }
    }

    @GetMapping("/page")
    public ApiResponse<Page<DeviceVO>> page(DeviceQueryDTO dto) {
        try {
            return ApiResponse.<Page<DeviceVO>>ok()
                    .data(deviceService.page(dto))
                    .build();
        } catch (Exception e) {
            return ApiResponse.<Page<DeviceVO>>error().msg("分页查询设备信息失败: " + e.getMessage()).build();
        }
    }

    @GetMapping("/custom/page")
    public ApiResponse<Page<DeviceVO>> customPage(DeviceQueryDTO dto) {
        try {
            return ApiResponse.<Page<DeviceVO>>ok().data(deviceService.customPage(dto)).build();
        } catch (Exception e) {
            return ApiResponse.<Page<DeviceVO>>error().msg("自定义分页查询设备信息失败: " + e.getMessage()).build();
        }
    }

    @DeleteMapping
    public ApiResponse<Object> delete(@RequestParam("id") Long id) {
        try {
            deviceService.deleteById(id);
            return ApiResponse.ok().build();
        } catch (Exception e) {
            return ApiResponse.error().msg("删除设备信息失败: " + e.getMessage()).build();
        }
    }

    @GetMapping
    public ApiResponse<Object> selectById(@RequestParam("id") Long id) {
        try {
            return ApiResponse.ok().data(deviceService.selectById(id)).build();
        } catch (Exception e) {
            return ApiResponse.error().msg("查询设备信息失败: " + e.getMessage()).build();
        }
    }

    @PutMapping
    public ApiResponse<Object> update(@RequestBody DeviceDTO dto) {
        try {
            return ApiResponse.ok().data(deviceService.update(dto)).build();
        } catch (Exception e) {
            return ApiResponse.error().msg("更新设备信息失败: " + e.getMessage()).build();
        }
    }

}